package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;

import com.alee.utils.swing.Timer;
import com.it2297.controller.JobController;
import com.it2297.main.CompanyApp;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.DefaultComboBoxModel;
import java.awt.SystemColor;

public class JobViewJobApplicants extends JFrame {

	private JPanel contentPane;
	private JTextField tf_title;
	private JComboBox cb_applicants;
	private JButton btnGet;
	private JLabel lbl;
	private JLabel lblApplicants;
	final JobController jct= new JobController(this);
	private JLabel lbldate;
	private JTextField tf_date;
	private JTextField tf_name;
	private JTextField tf_email;
	private JTextField tf_phone;
	private JTextField tf_cname;
	private JLabel lblname;
	private JLabel lblemail;
	private JLabel lblphone;
	private JLabel lblcname;
	private String noResult;
	private JButton btnDone;
	private String jobId;
	private JTextField tf_jobId;
	private JLabel lblJobId;
	private JTextArea tf_info;
	private JLabel lblinfo;
	private JButton btnShortlistApplicant;
	
	
	final static int interval = 100;
	private int i;
	private JProgressBar pb;
	private Timer timer;
	private JLabel label;
	
	public String getJobTitle(){
		return tf_title.getText();
	}	
	public void setJobTitle(String jobTitle){
		tf_title.setText(jobTitle);
	}
	public String getNRIC(){
		return cb_applicants.getSelectedItem().toString();
	}	
	public void setDate(String date){
		tf_date.setText(date);
	}	
	public void setName(String name){
		tf_name.setText(name);
	}	
	public void setEmail(String email){
		tf_email.setText(email);
	}
	public void setPhone(String phone){
		tf_phone.setText(phone);
	}
	public void setCompanyName(String cname){
		tf_cname.setText(cname);
	}
	public void setNoResult(String noResult){
		this.noResult = noResult;
	}
	public void setJobId(String jobId){
		tf_jobId.setText(jobId);
	}
	public String getJobId(){
		return tf_jobId.getText();
	}
	public void setAdditionalInfo(String info){
		tf_info.setText(info);
	}
	/**
	 * Launch the application.
	 */
/*	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JobViewJobApplicantsTest frame = new JobViewJobApplicantsTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/

	/**
	 * Create the frame.
	 */
	public JobViewJobApplicants() {
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 740, 560);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("View Job Applicants");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 35));
		lblNewLabel.setBounds(10, 11, 704, 63);
		contentPane.add(lblNewLabel);
		
		JLabel lblJobTitle = new JLabel("Job Title:");
		lblJobTitle.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblJobTitle.setBounds(134, 85, 75, 25);
		contentPane.add(lblJobTitle);
		
		tf_title = new JTextField();
		tf_title.setEditable(false);
		tf_title.setBounds(207, 87, 331, 25);
		contentPane.add(tf_title);
		tf_title.setColumns(10);
		
		btnGet = new JButton("Get Applicants");
		btnGet.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cb_applicants.revalidate();
				for(int i=0; i<jct.getApplicants().size();i++){
					cb_applicants.addItem(jct.getApplicants().get(i));
				}
					pb.setVisible(true);
					btnGet.setEnabled(false);
					i = 0;
					label.setVisible(true);
					String str = "<html>" + "<font color=\"#008000\">" + "<b>" + "Getting applicants......." + "</b>" + "</font>" + "</html>";
					label.setText(str);
					timer.start();			
			}
		});
		btnGet.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnGet.setBounds(392, 117, 146, 32);
		contentPane.add(btnGet);
		
		cb_applicants = new JComboBox();
		cb_applicants.addItem("------------------Select an applicant--------------");
		cb_applicants.setVisible(false);
		cb_applicants.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jct.getApplicantDetails();
				if(cb_applicants.getSelectedItem()=="------------------Select an applicant--------------"){
					tf_phone.setText(null);
					tf_cname.setText(CompanyApp.getCurrentUser().getCompanyName());
				}
			}
		});
		cb_applicants.setBounds(329, 232, 263, 25);
		contentPane.add(cb_applicants);
		
		lbl = new JLabel("");
		lbl.setHorizontalAlignment(SwingConstants.CENTER);
		lbl.setFont(new Font("Times New Roman", Font.BOLD, 13));
		lbl.setBounds(10, 160, 704, 25);
		contentPane.add(lbl);
		
		lblApplicants = new JLabel("");
		lblApplicants.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblApplicants.setBounds(134, 232, 196, 25);
		contentPane.add(lblApplicants);
		
		lbldate = new JLabel("Application Date/Time:");
		lbldate.setVisible(false);
		lbldate.setFont(new Font("Tahoma", Font.BOLD, 15));
		lbldate.setBounds(134, 266, 196, 25);
		contentPane.add(lbldate);
		
		tf_date = new JTextField();
		tf_date.setEditable(false);
		tf_date.setVisible(false);
		tf_date.setBounds(329, 268, 220, 25);
		contentPane.add(tf_date);
		tf_date.setColumns(10);
		
		tf_name = new JTextField();
		tf_name.setEditable(false);
		tf_name.setVisible(false);
		tf_name.setBounds(329, 302, 263, 25);
		contentPane.add(tf_name);
		tf_name.setColumns(10);
		
		tf_email = new JTextField();
		tf_email.setEditable(false);
		tf_email.setVisible(false);
		tf_email.setBounds(329, 338, 201, 25);
		contentPane.add(tf_email);
		tf_email.setColumns(10);
		
		tf_phone = new JTextField();
		tf_phone.setEditable(false);
		tf_phone.setVisible(false);
		tf_phone.setBounds(329, 374, 118, 25);
		contentPane.add(tf_phone);
		tf_phone.setColumns(10);
		
		tf_cname = new JTextField();
		tf_cname.setText(CompanyApp.getCurrentUser().getCompanyName());
		tf_cname.setEditable(false);
		tf_cname.setVisible(false);
		tf_cname.setBounds(329, 196, 287, 25);
		contentPane.add(tf_cname);
		tf_cname.setColumns(10);
		
		lblname = new JLabel("Applicant Name:");
		lblname.setVisible(false);
		lblname.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblname.setBounds(134, 300, 196, 25);
		contentPane.add(lblname);
		
		lblemail = new JLabel("Applicant Email:");
		lblemail.setVisible(false);
		lblemail.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblemail.setBounds(134, 336, 196, 25);
		contentPane.add(lblemail);
		
		lblphone = new JLabel("Applicant Phone:");
		lblphone.setVisible(false);
		lblphone.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblphone.setBounds(134, 372, 196, 25);
		contentPane.add(lblphone);
		
		lblcname = new JLabel("Job's Company:");
		lblcname.setVisible(false);
		lblcname.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblcname.setEnabled(true);
		lblcname.setBounds(134, 194, 196, 25);
		contentPane.add(lblcname);
		
		btnDone = new JButton("Back");
		btnDone.setVisible(false);
		btnDone.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeWindow();
			}
		});
		btnDone.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnDone.setBounds(579, 480, 118, 32);
		contentPane.add(btnDone);
					//50 for each shifting of i
		 timer = new Timer(50, new ActionListener() {
			  public void actionPerformed(ActionEvent evt) {
					  if (i == 100){
						  timer.stop();
						  btnGet.setEnabled(true);
						  pb.setValue(0);
						  pb.setVisible(false);
						  label.setVisible(false);
						  label.setText("");
  
						  if(noResult==""){
								JOptionPane.showMessageDialog(null, "There are currently no pending applicantions for the \"" + tf_title.getText().toString() + "\" job.");
								lbl.setText("");
								lblApplicants.setText("");
								cb_applicants.setVisible(false);
								lbldate.setVisible(false);
								tf_date.setVisible(false);
								tf_name.setVisible(false);
								tf_email.setVisible(false);
								tf_phone.setVisible(false);
								tf_cname.setVisible(false);
								lblname.setVisible(false);
								lblemail.setVisible(false);
								lblphone.setVisible(false);
								lblcname.setVisible(false);
								lblinfo.setVisible(false);
								tf_info.setVisible(false);
								btnShortlistApplicant.setVisible(false);
								closeWindow();
							}
							else{
								lbl.setText("Currently viewing applicants who applied for the '" + tf_title.getText() + "' job.");
								int count = cb_applicants.getItemCount()-1;
								lblApplicants.setText("Showing " + count + " applicant(s): ");
								btnGet.setEnabled(false);
								
								btnDone.setVisible(true);
								cb_applicants.setVisible(true);
								lbldate.setVisible(true);
								tf_date.setVisible(true);
								tf_name.setVisible(true);
								tf_email.setVisible(true);
								tf_phone.setVisible(true);
								tf_cname.setVisible(true);
								lblname.setVisible(true);
								lblemail.setVisible(true);
								lblphone.setVisible(true);
								lblcname.setVisible(true);
								lblinfo.setVisible(true);
								tf_info.setVisible(true);
								btnShortlistApplicant.setVisible(true);
							}						
					  }
				  i = i + 1; //i++
				  pb.setValue(i);
			  	}
			  });
		
		tf_jobId = new JTextField();
		tf_jobId.setEditable(false);
		tf_jobId.setBounds(207, 123, 50, 25);
		contentPane.add(tf_jobId);
		tf_jobId.setColumns(10);
		
		lblJobId = new JLabel("Job ID:");
		lblJobId.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblJobId.setBounds(134, 121, 75, 25);
		contentPane.add(lblJobId);
		
		lblinfo = new JLabel("Additional Info:");
		lblinfo.setVisible(false);
		lblinfo.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblinfo.setBounds(134, 408, 196, 25);
		contentPane.add(lblinfo);
		
		pb = new JProgressBar();
		pb.setVisible(false);
		pb.setValue(0);
		pb.setStringPainted(true);
		pb.setBounds(278, 166, 314, 19);
		contentPane.add(pb);
		
		tf_info = new JTextArea();
		tf_info.setBackground(SystemColor.control);
		tf_info.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
		tf_info.setEditable(false);
		tf_info.setVisible(false);
		tf_info.setBounds(329, 410, 368, 59);
		contentPane.add(tf_info);
		
		btnShortlistApplicant = new JButton("Shortlist current applicant");
		btnShortlistApplicant.setVisible(false);
		btnShortlistApplicant.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				if(cb_applicants.getSelectedItem()=="------------------Select an applicant--------------"){
					JOptionPane.showMessageDialog(null, "Please select an applicant!");
				}
				else{
					int option = JOptionPane.showConfirmDialog (null, "Are you sure you want to shortlist this applicant?",null, JOptionPane.YES_NO_OPTION);
					if (option == JOptionPane.YES_OPTION ) {
						
						jct.shortlistApplicant();
						JOptionPane.showMessageDialog(null, "Applicant shortlisted! Send an email to notify the applicant.");
						cb_applicants.removeItem(cb_applicants.getSelectedItem().toString());
						int count = cb_applicants.getItemCount()-1;
						lblApplicants.setText("Showing " + count + " applicant(s): ");
						cb_applicants.setSelectedItem("------------------Select an applicant--------------");
							if(cb_applicants.getItemCount()==1){
								JOptionPane.showMessageDialog(null, "There are currently no other pending applications for this job!");
								closeWindow();
							}
							
						CompanySendEmail cse = new CompanySendEmail();
						cse.setLocationRelativeTo(null);
						cse.setVisible(true);
					}
					else {
						JOptionPane.showMessageDialog(null, "Cancelled.");
					}
				}
			}
		});
		btnShortlistApplicant.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnShortlistApplicant.setBounds(329, 480, 240, 32);
		contentPane.add(btnShortlistApplicant);
		
		label = new JLabel("");
		label.setFont(new Font("Tahoma", Font.BOLD, 13));
		label.setVisible(false);
		label.setBounds(126, 166, 152, 19);
		contentPane.add(label);
	}
	public void closeWindow(){
		this.dispose();
	}
}
